package com.ag.wsq.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import jxl.Workbook;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.ag.common.BaseAction;
import com.ag.wsq.service.impl.UploadExcleServiceImpl;

/*
 * @author 王淑其
 */

public class UploadExcleAction extends BaseAction {
	private static final Logger logger = Logger.getLogger(UploadExcleAction.class);

	UploadExcleServiceImpl uploadExcleServiceImpl;
	private File file;

	public String upload() throws Exception {
		/**
		 * 1、拿到上传文件 2、解析内容 3、写入数据库
		 */

		HttpServletRequest request = ServletActionContext.getRequest();
		String message = null;
		Workbook personWb = null;

		InputStream is = new FileInputStream(getFile());
		if (file != null && file.length()>0) {
			personWb = Workbook.getWorkbook(is);
		}
		
		try {
			message = uploadExcleServiceImpl.saveExcel(personWb);
		} catch (Exception e) {
			e.printStackTrace();
			message = "提交失败，请检查数据是否错误";
		}
		System.out.println("-----start:save()-----");
		System.out.println("打印出："+message);
		request.setAttribute("message", message);
		is.close();

		return "uploadOK";

	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public UploadExcleServiceImpl getUploadExcleServiceImpl() {
		return uploadExcleServiceImpl;
	}

	public void setUploadExcleServiceImpl(
			UploadExcleServiceImpl uploadExcleServiceImpl) {
		this.uploadExcleServiceImpl = uploadExcleServiceImpl;
	}

}
